<?php

namespace app\api\model\stall;


use app\api\model\order\Order;
use app\api\model\plus\discount\Product as DiscountProductModel;
use app\supplier\model\stall\Category as StallCategoryModel;
use app\common\model\product\Category as CategoryModel;
use app\common\model\product\Product as ProductModel;
use app\common\model\stall\Product as StallProductModel;

class Product extends ProductModel
{
    public function getProcuteList($stall_id = 0, $parentId = 0, $shop_supplier_id = 0)
    {
        $outary = [];
        $model = new StallCategoryModel();
        $list = $model->getStallCateTree($stall_id, $shop_supplier_id);
        // 二级分类
        $cateary = CategoryModel::smallNameList($parentId, $list);
        foreach ($cateary as $key => $value) {
            $outary[$key]['category'] = $value;
            $param = [
                'list_rows' => 9999,
                'category_id' => $value['category_id'],
                'sortType' => 'name',
                'status' => -1,
                'shop_supplier_id' => $shop_supplier_id
            ];
            $model = new ProductModel();
            $alist = $model->getListNoCity($param);
            $indatalist = (empty($alist)) ? [] : $alist->toarray();
            $datalist = $indatalist['data'];

            $discount_product_data = (new DiscountProductModel())->getDiscountAtivityProduct(); //限时优惠商品id
            foreach ($datalist as $akey => $avalue) {
                $newrs = self::getStallProductStatus($stall_id, $avalue['product_id']);
                if ($newrs) {
                    if ($newrs['is_show'] == 0) {
                        unset($datalist[$akey]);
                        continue;
                    }
                    $datalist[$akey]['product_price'] = bcadd($avalue['product_price'], $newrs['product_price'], 2);
                }
                $datalist[$akey]['discount_product_data'] = [];
                if (isset($discount_product_data[$avalue['product_id']]) && $discount_product_data[$avalue['product_id']]) {
                    $datalist[$akey]['discount_product_data'] = $discount_product_data[$avalue['product_id']];
                    $datalist[$akey]['activity_type'] = 'discount'; //限时优惠
                } else {
                    $datalist[$akey]['activity_type'] = 'putong'; //未参加活动的商品
                }
            }
            $datalist = array_values($datalist);
            $outary[$key]['list'] = $datalist;
        }
        return $outary;
    }

    private static function getStallProductStatus($stall_id, $productid = 0)
    {
        return StallProductModel::checkDB($stall_id, $productid);
    }

    public static function getStallSaleTime($stall_id=0,$timetype=0,$type=0){
        if($timetype==0){
            return self::getStallSale($stall_id,[],$type);
        }elseif($timetype==1){
            //月
            $start_time = strtotime(date("Y-m-1",time()));
            $end_time = time();

            $params = [];
            $params[] = ['create_time','>=',$start_time];
            $params[] = ['create_time','<',$end_time];
            return self::getStallSale($stall_id,$params,$type);
        }elseif($timetype==2){
            //日
            $start_time = strtotime(date("Y-m-d",time()));
            $end_time = time();

            $params = [];
            $params[] = ['create_time','>=',$start_time];
            $params[] = ['create_time','<',$end_time];
            return self::getStallSale($stall_id,$params,$type);
        }
    }

    public static function getStallSale($stall_id,$where=[],$type=0){
        $Order = new Order();
        if($where){
            $Order->where($where);
        }
        if($type==0){
            return $Order->where('stall_id', '=', $stall_id)
                ->where('order_status', '=', 30)
                ->where('is_order_refund', '<>', 2)
                ->count();
        }else{
            $moeny = $Order->where('stall_id', '=', $stall_id)
                ->where('order_status', '=', 30)
                ->where('is_order_refund', '<>', 2)
                ->sum("pay_price");
            return bcmul($moeny,1,2);
        }
    }
}